import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Simple server program which sends a string to the client once the connection
 * has established.
 * 
 * @author imnak0
 * 
 */
public class AdviceServer {
	private final String[] adviceList = { "I miss you",
			"How could you betray me?",
			"can't believe you've been cheating on me", "I'm still loving you",
			"Please come back to me", "I'll forgive you" };

	public void go() {
		System.out.println("Start server");
		
		try {
			ServerSocket serverSocket = new ServerSocket(2121);

			while (true) {
				Socket socket = serverSocket.accept();

				PrintWriter writer = new PrintWriter(socket.getOutputStream());
				String advice = getAdvice();
				writer.println(advice);
				
				System.out.print("Message sent : ");
				System.out.println(advice);
				writer.close();
			}
		} catch (IOException e) {
			System.out.println("IOExeption occured");
			e.printStackTrace();
		}
		
		System.out.println("Finish server");
	}

	/**
	 * get one of the string from the list
	 */
	private String getAdvice() {
		int random = (int) (Math.random() * adviceList.length);
		return adviceList[random];
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		AdviceServer server = new AdviceServer();
		server.go();
	}

}
